//
//  ViewController.swift
//  jssdkDemo
//
//  Created by cyptt on 2024/5/8.
//

import UIKit

//
//  ViewController.swift
//  bridging_js
//
//  Created by cyptt on 2021/7/23.
//

import UIKit
import WebKit
import Alamofire
import CHAFNetwork
import CHJsSdk
let G_WEB:String = "G_WEB"
let GO_LOCAL_WEB:String = "GO_LOCAL_WEB"
let GET:String = "GET"
let POST:String = "POST"
let UPLOAD:String = "UPLOAD"
let UPLOADS:String = "UPLOADS"
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,CHWebDelegate {
   
    
    @IBOutlet weak var goBtn: UIButton!
    @IBOutlet weak var textF: UITextField!
    @IBOutlet weak var tableView: UITableView!
    var webView = WKWebView()
    lazy var dataArr:[ String] = {
        let arr:[ String] = [G_WEB,GO_LOCAL_WEB,GET,POST,UPLOAD,UPLOADS]
        return arr
    }()
  
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(webView)
//        self .btn2Click(self)
        //分支----
        
        self.tableView.delegate = self
        self.tableView.dataSource = self

     
    }
    
    @IBAction func goBtnClick(_ sender: Any) {
        
        

        var urlStr:String = self.textF.text ?? "";
//        let urlStr:String = "https://www.baidu.com/";
        urlStr = "https://www.baidu.com"
        
        let webController = CHWebViewController.init(url: urlStr,isLocal: false,webDelegate: self);
        self.navigationController?.pushViewController(webController, animated: true)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "CELL")
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "CELL")
        }
        cell?.textLabel?.text = self.dataArr[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let itemName = self.dataArr[indexPath.row]
       
        if itemName == G_WEB {
            goweb()
        }else if itemName == GO_LOCAL_WEB{
            goLocalkWeb()
        }else if itemName == GET{
            get()
        }else if itemName == POST{
            post()
        }else if itemName == UPLOAD{
            upload()
        }else if itemName == UPLOADS{
            uploads()
        }
    }
 
    func goweb() {

        let urlStr:String = "http://172.18.89.50:9101/index_jssdk.html";
//        let urlStr:String = "https://www.baidu.com/";
        self.navigationController?.pushViewController(CHWebViewController.init(url: urlStr,isLocal: false,webDelegate: self), animated: true)
    }
    
    func goLocalkWeb() {
        
        let htmlPath = Bundle.main.path(forResource: "index_jssdk", ofType: "html")
        print(htmlPath)
        let htmlContent = try!String.init(contentsOfFile: htmlPath!)

        self.navigationController?.pushViewController(CHWebViewController.init(url: htmlContent,isLocal: true,webDelegate: self), animated: true)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    
    
  
    func get() {

        let params = ["userName":"成都","age":"f51d665d9242ce03a07dde11280ae133"]
        let urlSting:String = "http://192.168.1.59:8080/user/test";

        
        CHNetManage.GET(url: urlSting, param: params) { (result) in
            print(result)
        } failure: { (errStr) in
            print(errStr)
        }

    }
    
    func post(){
       
        let params = ["userName":"adbc","age":"f51d665d9242ce03a07dde11280ae133"]
        let urlSting:String = "http://192.168.1.59:8080/user/testJson";
        CHNetManage.POST(url: urlSting, param: params,cTypeJson: true) { (result) in
            print(result)
        } failure: { (errStr) in
            print(errStr)
        }
    }
    
     func upload() {
        let urlSting:String = "http://192.168.1.74:8080/user/upload";
        let fileUrl:URL? = Bundle.main.url(forResource: "back_grey_scca_001", withExtension: "png")
        var fileData:Data? = Data.init()
         do {
            
            if fileUrl != nil {
                fileData = try Data.init(contentsOf: fileUrl!)
                print(fileData!)
            }
        } catch{
            
        }
         CHNetManage.uploadFile(url: urlSting, imageData: fileData!, parameters: ["name":"name001"],headerDics: nil) { result in
             
         }failure: { errMsg in
             
         }
//
//
      
    }
   

     func uploads() {
        let urlSting:String = "http://192.168.1.59:8080/user/uploadFiles";
        var datas:[Data]? = []
        let fileUrl:URL? = Bundle.main.url(forResource: "back_grey_scca_001", withExtension: "png")
        for i in 1...2{
            var fileData:Data? = Data.init()
             do {
                
                if fileUrl != nil {
                    fileData = try Data.init(contentsOf: fileUrl!)
                    print(fileData!)
                    datas!.append(fileData!)
                }
            } catch{
                
            }
        }
       
       

         CHNetManage.uploadFiles(url: urlSting, imageDatas: datas!, parameters: [:],headerDics: nil) { result in
             
         }failure: { errMsg in
             
         }
    }
    
    

 
}

extension ViewController{
   
    
    
}





